package com.excesys;

import cn.hutool.core.lang.Pair;

import java.util.ArrayList;
import java.util.List;

public class FindCloseElements {

    public static List<Pair<Integer, Integer>> findCloseElements(List<Integer> list1, List<Integer> list2) {
        List<Pair<Integer, Integer>> closeElements = new ArrayList();
        int i = 0, j = 0;

        while (i < list1.size() && j < list2.size()) {
            int diff = Math.abs(list1.get(i) - list2.get(j));

            if (diff <= 15) {
                closeElements.add(new Pair<>(list1.get(i), list2.get(j)));
                i++;
                j++;
            } else if (list1.get(i) < list2.get(j)) {
                i++;
            } else {
                j++;
            }
        }

        return closeElements;
    }

    public static void main(String[] args) {
        // 假设你已经有了两个有序的整数列表list1和list2，这里仅作示例
        List<Integer> list1 = new ArrayList<>();
        List<Integer> list2 = new ArrayList<>();
        for (int i = 0; i < 30; i++) {
            list1.add(i);
            list2.add(i + 1);
        }
        List<Pair<Integer, Integer>> result = findCloseElements(list1, list2);
        for (Pair<Integer, Integer> pair : result) {
            System.out.println(pair);
        }
    }

}

